-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
response: parse: Include input JSON in Error, on JSON parsing errors #32
Conversation
For example, when the input is `["header", :{"return_code":-77}}`, a parse error. The error is then as follows. ``` `parse': unexpected token at ':{"return_code":-77}}' (JSON::ParserError) ``` Include the input JSON in the error since the error does not include the full input JSON, making it difficult to debug.
lib/groonga/client/response/base.rb
Outdated
if callback and | ||
/\A#{Regexp.escape(callback)}\((.+)\);\z/ =~ raw_response | ||
response = JSON.parse($1) | ||
response = json_parse.call($1) | ||
else | ||
response = JSON.parse(raw_response) | ||
response = json_parse.call(raw_response) | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Exception#exception
なんて初めて知ったよ!でも、ここでlambdaを使う必要はないんじゃないかな。$1
とraw_response
を変数に入れるだけで十分じゃない?
こんなんはどうかな?
diff --git a/lib/groonga/client/request/error.rb b/lib/groonga/client/request/error.rb
index e7ffb16..4bb344b 100644
--- a/lib/groonga/client/request/error.rb
+++ b/lib/groonga/client/request/error.rb
@@ -36,6 +36,21 @@ module Groonga
super(message)
end
end
+
+ class InvalidResponse < Error
+ attr_reader :command
+ attr_reader :raw_response
+ def initialize(command, raw_response, error_message)
+ @command = commadn
+ @raw_response = raw_response
+ message = +"invalid response: "
+ message << "#{command.command_name}: "
+ message << "#{error_message}: "
+ message << command.to_command_format
+ message << ": <#{raw_response}>"
+ super(message)
+ end
+ end
end
end
end
diff --git a/lib/groonga/client/response/base.rb b/lib/groonga/client/response/base.rb
index 2bb8f19..72154cd 100644
--- a/lib/groonga/client/response/base.rb
+++ b/lib/groonga/client/response/base.rb
@@ -73,9 +73,16 @@ module Groonga
callback = command["callback"]
if callback and
/\A#{Regexp.escape(callback)}\((.+)\);\z/ =~ raw_response
- response = JSON.parse($1)
+ json = JSON.parse($1)
else
- response = JSON.parse(raw_response)
+ json = JSON.parse(raw_response)
+ end
+ begin
+ response = JSON.parse(json)
+ rescue JSON::ParseError => error
+ raise InvalidResponse.new(command,
+ raw_response,
+ "invalid JSON: #{error}")
end
if response.is_a?(::Array)
header, body = response
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
差分が極小になる方向で頑張りましたが、 InvalidResponse
を追加する感じに変更しました!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM 👍🏾
lib/groonga/client/response/error.rb
Outdated
@@ -66,6 +67,21 @@ def line | |||
end | |||
end | |||
end | |||
|
|||
class InvalidResponse < Client::Error |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
このファイルって例外的なレスポンス用のファイルじゃなくて、想定内のエラーレスポンス用のファイルだから、例外的なことをここのファイルには入れないほうがいいかな。
Groonga::Client::Response
の下に置くのはやり過ぎな感じがするし、lib/groonga/client/error.rb
でGroonga::Client
の下でいいかな。
そうすると、lib/groonga/client/request/error.rb
もlib/groonga/client/error.rb
に移動して、Groonga::Client
直下にしたくなるね。
diff --git a/lib/groonga/client/error.rb b/lib/groonga/client/error.rb
index 6ec96d8..43fd601 100644
--- a/lib/groonga/client/error.rb
+++ b/lib/groonga/client/error.rb
@@ -18,5 +18,20 @@ module Groonga
class Client
class Error < StandardError
end
+
+ class ErrorResponse < Error
+ attr_reader :response
+ def initialize(response)
+ @response = response
+ command = @response.command
+ status_code = @response.status_code
+ error_message = @response.error_message
+ message = "failed to execute: "
+ message << "#{command.command_name}: #{status_code}: "
+ message << "<#{error_message}>: "
+ message << command.to_command_format
+ super(message)
+ end
+ end
end
end
diff --git a/lib/groonga/client/request/error.rb b/lib/groonga/client/request/error.rb
index e7ffb16..e7678ed 100644
--- a/lib/groonga/client/request/error.rb
+++ b/lib/groonga/client/request/error.rb
@@ -19,23 +19,9 @@ require "groonga/client/error"
module Groonga
class Client
module Request
- class Error < Client::Error
- end
-
- class ErrorResponse < Error
- attr_reader :response
- def initialize(response)
- @response = response
- command = @response.command
- status_code = @response.status_code
- error_message = @response.error_message
- message = "failed to execute: "
- message << "#{command.command_name}: #{status_code}: "
- message << "<#{error_message}>: "
- message << command.to_command_format
- super(message)
- end
- end
+ # For backward compatibility
+ Error = Client::Error
+ ErrorResponse = Client::ErrorResponse
end
end
end
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ナルホド。そっちのエラーだったのか。
lib/groonga/client/response/error.rb
Outdated
message << command.to_command_format | ||
message << ": <#{raw_response}>" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
実際のメッセージを見ると読みにくいからちょっと印を入れようか。
message << command.to_command_format | |
message << ": <#{raw_response}>" | |
message << "<#{command.to_command_format}>: " | |
message << "<#{raw_response}>" |
test/response/test-base.rb
Outdated
err = assert_raise(Groonga::Client::Response::InvalidResponse) do | ||
Groonga::Client::Response::Base.parse(command, raw_response) | ||
end | ||
error_message = <<~'MESSAGE'.chomp | ||
invalid response: cancel: invalid JSON: unexpected token at ':{"return_code":-77}}': cancel: <["header", :{"return_code":-77}}> | ||
MESSAGE | ||
assert_equal(error_message, err.message) | ||
assert_equal(command, err.command) | ||
assert_equal(raw_response, err.raw_response) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
これでいける?
err = assert_raise(Groonga::Client::Response::InvalidResponse) do | |
Groonga::Client::Response::Base.parse(command, raw_response) | |
end | |
error_message = <<~'MESSAGE'.chomp | |
invalid response: cancel: invalid JSON: unexpected token at ':{"return_code":-77}}': cancel: <["header", :{"return_code":-77}}> | |
MESSAGE | |
assert_equal(error_message, err.message) | |
assert_equal(command, err.command) | |
assert_equal(raw_response, err.raw_response) | |
begin | |
JSON.parse(raw_response) | |
rescue JSON::ParseError => error | |
parse_error_message = error.to_s | |
end | |
error = Groonga::Client::InvalidResponse.new(command, raw_response, parse_error_message) | |
assert_raise(error) do | |
Groonga::Client::Response::Base.parse(command, raw_response) | |
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
いけました。
For example, when the input is
["header", :{"return_code":-77}}
, a parse error.The error is then as follows.
Include the input JSON in the error since the error does not include the full input JSON, making it difficult to debug.